// pages/menu/menu.js
const util = require("../../utils/request");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dishes: [], // 菜品信息
    categoryData: [], // 类别信息
    categoryId: 0, // 已选类别id
    originPrice: 0, // 原价
    price: 0, // 应付价格
    show: false, // 显示抽屉
    seatId: "", // 餐桌id
    isMember: false, // 是否为会员
    member: {} // 会员信息
  },

  /**
   * 切换菜单触发函数
   */
  onChangeMenu(event) {
    let index = event.detail;
    let onCategoryId = this.data.categoryData[index].categoryId;
    this.setData({
        categoryId: onCategoryId
    });
    // this.getDishesByCategoryId();
  },

  /**
   * 改变数量触发函数
   */
  onChange(event) {
    let index = event.currentTarget.dataset.id;
    let orderNum = event.detail;
    let dishes = this.data.dishes;
    dishes[index].orderNum = orderNum;
    this.setData({
        dishes: dishes
    });
    this.computePrice();
  },

  /**
   * 计算价格
   */
  computePrice() {
    let dishes = this.data.dishes;
    let originPrice = 0;
    for(let index in dishes) {
        if(dishes[index].orderNum > 0) {
            originPrice += dishes[index].orderNum * dishes[index].price;
        }
    }
    this.setData({
        originPrice: originPrice
    });
    if(this.data.isMember) {
        this.setData({price: originPrice * this.data.member.discount});
    } else {
        this.setData({price: originPrice});
    }
  },

  /**
   * 关闭确定订单按钮触发的抽屉
   */
  onClose() {
    this.setData({ show: false });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const seatId = wx.getStorageSync('seatId');
    if(seatId) {
        this.setData({
            seatId: seatId
        });
    }
    this.getMemberByWxId();
  },

  /**
   * 查询会员信息
   */
  getMemberByWxId() {
    const openid = wx.getStorageSync('openid');
    util.request("/rms/member/getInfoByWxId/" + openid, 'GET', {}, res => {
        let code = res.data.code;
        if(code == 200) {
            let member = res.data.data;
            if(member == null) {
                this.setData({isMember: false});
            } else {
                this.setData({
                    isMember: true,
                    member: member
                });
            }
        }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getCategory();
    this.getDishesByCategoryId();
  },

  /**
   * 查询菜单
   */
  getCategory() {
    util.request("/rms/category/allData", "GET", {}, res => {
        let category = res.data.data;
        let newCategory = {
            categoryId: 0,
            categoryName: '推荐组合'
        };
        category.unshift(newCategory);
        this.setData({
            categoryData: res.data.data
        });
    })
  },

  /**
   * 查询菜品
   */
  getDishesByCategoryId() {
    this.setData({
        dishes: []
    });
    util.request("/rms/dishes/allDataByRecommend", "GET", {}, res => {
        let dishesData = res.data.data;
        for(let index in dishesData) {
            if(dishesData[index].status < '3') {
                dishesData[index].pictureUrl = 'http://localhost:8091/profile/upload' + dishesData[index].pictureUrl
                this.setData({
                    dishes: this.data.dishes.concat(dishesData[index])
                })
            }
        }
    })
  },

  /**
   * 确认订单触发按钮
   */
  onClickEnsureButton() {
    this.setData({ show: true });
  },

  /**
   * 触发提交按钮
   */
  onClickSubmitButton() {
    this.setData({ show: false });
    this.submit();
    this.reset();
    wx.redirectTo({
        url: '../index/index',
    })
  },

  /**
   * 提交订单业务
   */
  submit() {
    const orderId = wx.getStorageSync('orderId');
    let dishes = this.data.dishes;
    let dishesOrderMap = new Map();
    for(let index in dishes) {
        if(dishes[index].orderNum > 0) {
            dishesOrderMap.set(dishes[index].dishesId, dishes[index].orderNum);
        }
    }
    //Map转为Json
    let obj= new Object();
    for (let[k,v] of dishesOrderMap) {
        obj[k] = v;
    }
    util.request('/rms/dishesOrder/' + orderId, 'POST', obj, res => {});
  },

  reset() {
      this.setData({
          price: 0,
          originPrice: 0
      });
      let dishes = this.data.dishes;
      for(let index in dishes) {
          if(dishes[index].orderNum > 0) {
            dishes[index].orderNum = 0;
          }
      }
      this.setData({
          dishes: dishes
      });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})